package com.ht.medicaltreatment.fragment

import android.content.Intent
import android.os.Environment
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.FileUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseFragment
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.activity.PostFileResultSampleListActivity
import com.ht.medicaltreatment.adapter.FileListAdapter
import com.ht.medicaltreatment.bean.FilePathBean
import com.ht.medicaltreatment.view.DownloadSuccessDialog
import com.ht.medicaltreatment.viewmodel.SampleViewModl
import kotlinx.android.synthetic.main.fragment_mulite_create_sample.btnDownload
import kotlinx.android.synthetic.main.fragment_mulite_create_sample.recyclerView
import kotlinx.android.synthetic.main.fragment_mulite_create_sample.tvPostFile
import kotlinx.android.synthetic.main.fragment_mulite_create_sample.viewBg
import java.io.File


//创建样本 - 批量创建
class MultiCreateSampleFragment:BaseFragment(), View.OnClickListener, OnItemClickListener {

    private lateinit var mModel:SampleViewModl
    private val file = File(Environment.getExternalStorageDirectory(), "惠通医疗")
    private lateinit var itemAdapter: FileListAdapter

    override fun getLayoutResId() = R.layout.fragment_mulite_create_sample

    override fun init() {
        mModel = ViewModelProvider(this)[SampleViewModl::class.java]
        btnDownload.setOnClickListener(this)
        tvPostFile.setOnClickListener(this)
        viewBg.setOnClickListener(this)
        FileUtils.createOrExistsDir(file)
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        itemAdapter = FileListAdapter()
        recyclerView.adapter = itemAdapter
        itemAdapter.setOnItemClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v){
            btnDownload -> {
                showToast("开始下载")
                btnDownload.text = "下载：0%"
                mModel.download{
                    btnDownload.text = "下载：$it%"
                    if(it == 100){
                        btnDownload.text = "下载完成"
                        DownloadSuccessDialog(requireContext()).show()
                    }
                }
            }

            viewBg -> hide()

            //导入
            tvPostFile -> {
                val list = FileUtils.listFilesInDir(file)
                val listFile = arrayListOf<FilePathBean>()
                list.forEach {
                    listFile += FilePathBean(it.name,it.absolutePath)
                }
                itemAdapter.setNewInstance(listFile)
                open()
            }
        }
    }

    fun open() {
        recyclerView.setVisibility(View.VISIBLE)
        viewBg.setVisibility(View.VISIBLE)
    }

    fun hide() {
        viewBg.setVisibility(View.GONE)
        recyclerView.setVisibility(View.GONE)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as FilePathBean
        val intent = Intent(requireContext(),PostFileResultSampleListActivity::class.java)
        intent.putExtra("path",item.path)
        startActivity(intent)
    }
}